//定义全局变量
var token = selectByKey("like_token");
var state = false;
var dirpath = 'http://m.example.com/likepnone/statics/images/trolley/';
var addArr = [];
var deliverId = null;
//添加地址模板
function addAddrFn(data, scriptdom){
    var mainmax = document.getElementById('main');
    var html = '';
    for (var k = 0; k < data.length; k++) {
        var htmlm = scriptdom.innerHTML;
        htmlm = htmlm.replace(/\{\{areaid\}\}/, data[k].area_id);
        htmlm = htmlm.replace(/\{\{name\}\}/, data[k].name);
        htmlm = htmlm.replace(/\{\{phone\}\}/g, data[k].phone);
        htmlm = htmlm.replace(/\{\{area\}\}/, data[k].full_name);
        htmlm = htmlm.replace(/\{\{address\}\}/, data[k].address);
        html += htmlm;
    }
    mainmax.innerHTML = html;
}
//切换地址模块
function switchAddr(){
    var fdiv = document.querySelectorAll(".addrList");
    //选择地址
    for(var i = 0; i < fdiv.length; i++){
        Tap(fdiv[i],function(e,dom){
            addArr = [];
            for(var j = 0; j < fdiv.length; j++){
                fdiv[j].children[0].src = dirpath + "Oval.png";
            }
            dom.children[0].src = dirpath + "iconfont-gou.png";
            addArr.push(dom.querySelector("span").innerHTML);
            addArr.push(dom.querySelector("i").innerHTML);
            addArr.push(dom.querySelector("s").innerHTML);
            addArr.push(dom.querySelector("em").innerHTML);
            deliverId = dom.getAttribute("data-area_id");
        })
    }
}
//提交订单模块
function submitOrder(){
    var content = document.querySelector(".contenter");
    var foot = document.querySelector("footer");
    var sub = document.querySelector("footer a");
    var addr = document.getElementById("main");
    var title = document.querySelector("header label");
    Tap(foot,function(){
        if(foot.children[0].innerHTML == "确认" && $("#main").css("display") == "block"){
            content.style.display = 'block';
            addr.style.display = "none";
            title.innerHTML = '确认订单';
            sub.innerHTML = "提交订单";
            //sub.src = "payorder.html";
            if(addArr.length>1){
                adressc.querySelector("span").innerHTML = addArr[0];
                adressc.querySelector("i").innerHTML = addArr[1];
                adressc.querySelector("s").innerHTML = addArr[2];
                adressc.querySelector("label").innerHTML = addArr[3];
                adressc.setAttribute("data-areaid",deliverId);
            }
        }else if(sub.innerHTML == "提交订单"){
            localStorage.setItem("deliverId",deliverId);
            window.location.href = "payorder.html";
        }
    });
}
window.onload = function(){
    var arrstr = localStorage.getItem("str");
    var arrObj = JSON.parse(arrstr);
    var listMode = document.getElementById("addrMod");
    var add = document.querySelector("header em");
    Tap(add,function(){
        window.location.href = "addaddress.html"
    });
    console.log(token);
    console.log(arrObj);
    $.ajax({
        url:pathName+"/api/v1/order/index",
        type:"POST",
        dataType:"json",
        headers:{
            "Accept":"application/json",
            "Authorization":"Bearer "+token,
        },
        data:{
            "buyGoods":arrObj
        },
        success:function(data){
            console.log(data);
            //chooseAddr();
            addAddrFn(data.addressDetail,listMode);
            //state = data.status;
            //submitOrder();
        }
    })
}